import pymysql

conn = pymysql.Connect(host='localhost', port=3306, user='root', password='root',
                       database='books', charset='utf8', autocommit=False)

cursor = conn.cursor()

# 增加
insert_sql = "insert into t_book(title, pub_date, `read`, `comment`) " \
             "values('Python从入门到放弃', '2020-01-01', 50, 0);"
cursor.execute(insert_sql)
print('受影响的行：', conn.affected_rows())
conn.commit()

# 修改
update_sql = "update t_book set `read` = 250 where title = 'Python从入门到放弃';"
cursor.execute(update_sql)
print('受影响的行：', conn.affected_rows())
conn.commit()

# 删除
delete_sql = "delete from t_book where title = 'Python从入门到放弃';"
cursor.execute(delete_sql)
print('受影响的行：', conn.affected_rows())
conn.commit()

# 查询
select_sql = "select id, title, pub_date from t_book"
cursor.execute(select_sql)
print(f'内容为：{cursor.fetchall()}')

cursor.close()

conn.close()
